<?php

namespace App\Http\Controllers\teacher;

use App\Model\Attendance;
use App\Model\AttendanceStudent;
use App\Model\ClassStudent;
use App\Model\Message;
use App\Model\Student;
use App\Model\Teacher;
use App\Model\Work;
use App\Model\WorkStudent;
use Carbon\Carbon;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Model\TheClass;
use Illuminate\Support\Facades\Auth;

class ClassController extends Controller
{
    public function index()
    {
        $teacherId = Auth::guard('teacher')->id();
        $classModel = new TheClass();
        $classes = $classModel->where('teacher_id', $teacherId)->get();
        foreach($classes as $k => $class) {
            $classes[$k]->student_count = ClassStudent::where('class_id', $class->id)->count();
        }
        return view('teacher.index', compact('classes'));
    }

    public function messagesPage($class_id)
    {
        $teacherId = Auth::guard('teacher')->id();
        $messageModel = new Message();
        $messages = $messageModel->where('class_id', $class_id)->orderBy('created_at')->get()->groupBy('student_id');
        //dd($messages->groupBy('student_id')->toArray());

        $classId = $class_id;
        return view('teacher.messages', compact('messages', 'class_id'));
    }

    public function messagePage($class_id, $student_id)
    {
        $teacherId = Auth::guard('teacher')->id();
        $messageModel = new Message();
        $messages = $messageModel->where('class_id', $class_id)->where('student_id', $student_id)->orderBy('created_at')->get();

        $classId = $class_id;
        return view('teacher.message', compact('messages', 'class_id', 'student_id'));
    }

    public function workPage($class_id)
    {
        $workModel = new Work();

        $workList = $workModel->where('class_id', $class_id)->orderBy('created_at', 'desc')->get();

        return view('teacher.work', compact('workList', 'class_id'));
    }

    public function checkWork($class_id, $work_id)
    {
        $workStudentModel = new WorkStudent();
        $classStudentModel = new ClassStudent();
        $studentModel = new Student();

        $studentArray = $classStudentModel->select(['student_id'])->where('class_id', $class_id)->get();
        $workStudentArray = $workStudentModel->select(['student_id'])->where('work_id', $work_id)->get();

        $result = $studentArray->diff($workStudentArray);

        $students = $studentModel->select(['name'])->whereIn('id', $result)->get();
        $strStudents = '还有 '.$students->implode('name', ', ').' 未交作业';


        return $this->apiResponse(200, $strStudents);


    }

    public function attendancePage($class_id)
    {
        $studentId = Auth::guard('student')->id();
        $attendanceModel = new Attendance();

        $attendance = $attendanceModel->where('class_id', $class_id)->where('status', 0)->first();
        $attendanceStatus = false;
        $strStudents = '';
        $attendanceId = 0;
        if ($attendance) {
            $attendanceStatus = true;
            $attendanceId = $attendance->id;
            $students = AttendanceStudent::select(['student_id'])->where('attendance_id', $attendanceId)->orderBy('created_at', 'desc')->get();
            $students = Student::select(['name'])->whereIn('id', $students)->get();
            $strStudents = '已签到的学生: '.$students->implode('name', ', ');
        }
        return view('teacher.attendance', compact('class_id', 'attendanceStatus', 'attendanceId', 'strStudents'));
    }

    public function startAttendance(Request $request)
    {
        $class_id = $request->input('class_id');
        $code = $request->input('code');
        $attendance = Attendance::create(['class_id' => $class_id, 'code' => $code]);


        return $this->apiResponse(200, '开始考勤');

    }

    public function stopAttendance(Request $request)
    {
        $attendance_id = $request->input('attendance_id');
        $attendance = Attendance::find($attendance_id);
        $attendance->status = 1;
        $attendance->save();

        return $this->apiResponse(200, '结束考勤');
    }

    public function sendMessage(Request $request, $class_id)
    {
        $student_id = $request->input('student_id');
        $content = $request->input('content');
        $messageModel = new Message();

        $message = $messageModel->create(['student_id' => $student_id, 'class_id' => $class_id, 'teacher_id' => 0, 'content' => $content]);

        if (!$message) {
            return $this->apiResponse(500, '服务器错误，请稍后重试');
        }
        return $this->apiResponse(200, '发送成功');
        
    }

    //2018-07-08 09:00:00
    public function createWork(Request $request, $class_id)
    {
        $expired_at = Carbon::parse($request->input('time'))->timestamp;
        //dd($expired_at);
        $work = Work::create([
            'title' => $request->input('title'),
            'remark' => $request->input('remark'),
            'class_id' => $class_id,
            'expired_at' => $expired_at,
        ]);

        if ($work) {
            return $this->apiResponse(200, '发布成功');
        }else {
            return $this->apiResponse(403, '发布失败');
        }
    }

    public function createClass(Request $request)
    {
        $teacherId = Auth::guard('teacher')->id();
        $invitationCode = strtoupper(str_random(6));

        $class = TheClass::create([
            'teacher_id' => $teacherId,
            'name' => $request->input('class_name'),
            'invitation_code' => $invitationCode,
        ]);
        if ($class) {
            return $this->apiResponse(200, '创建成功');
        }
        return $this->apiResponse(500, '创建失败');
    }



}
